package com.yangmin.rabbit.controller;


import com.yangmin.rabbit.config.RabbitConfig;
import com.yangmin.rabbit.pojo.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author diandian
 * @date 2021-12-11 18:28
 */
@RestController
public class PublisherController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/simpleString/{message}")
    public String simpleString(@PathVariable String message) {
        // 触发信息发送

        rabbitTemplate.convertAndSend(RabbitConfig.SIMPLE_QUEUE, message);

        return "ok";
    }


    @RequestMapping("/simpleObject")
    public String simpleObject() {
        // 触发信息发送
        User user = new User();
        user.setName("小白兔");
        user.setNote("小白兔  白又白");
        rabbitTemplate.convertAndSend(RabbitConfig.SIMPLE_QUEUE, user);
        return "ok";
    }

    /**
     *  Fanout广播模式
     * @param message
     * @return
     */
    @RequestMapping("/fanout/{message}")
    public String fanout(@PathVariable String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.FANOUT_EXCHANGE, null, message);
        return "ok";
    }


    /**
     *  路由模式  需要指定 routingKey
     */
    @RequestMapping("/direct/{routekey}/{message}")
    public String direct(@PathVariable String routekey ,@PathVariable String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE, routekey, message);
        return "ok";
    }

    /**
     *  主题模式  使用通配符进行 routingkey的指定
     */
    @RequestMapping("/topic/{routekey}/{message}")
    public String topic(@PathVariable("routekey") String routekey,@PathVariable("message")String message){
        rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE,routekey,message);
        return "ok";
    }
}
